<?php

namespace App\Http\Controllers\Edu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

use App\Repositories\Eloquent\Mgt\BranchRepository as Branch;
use App\Repositories\Eloquent\Edu\TypeRepository as Type;
use App\Repositories\Eloquent\Edu\TermRepository as Term;
use App\Repositories\Eloquent\Edu\CategoryRepository as Category;
use App\Repositories\Eloquent\Edu\ClaseRepository as Clase;
use App\Repositories\Eloquent\Edu\DeployRepository as Deploy;
use App\Repositories\Eloquent\Edu\TuserRepository as Tuser;

use App\Exports\ClaseExport;
use Maatwebsite\Excel\Facades\Excel;

class TrainController extends Controller
{

	protected $type, $branch, $term, $category, $clase, $deploy, $tuser;

    public function __construct(Branch $branch, Type $type, Tuser $tuser,
                Term $term, Category $category, Clase $clase, Deploy $deploy)
    {
        parent::__construct();

        $this->branch = $branch;

        $this->type = $type;

        $this->term = $term;

        $this->category = $category;

        $this->clase = $clase;

        $this->deploy = $deploy;

        $this->tuser = $tuser;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    	$typies = $this->type->index();

        return view($this->view, compact('typies'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $typies = $this->type->index();

        $branchs = $this->branch->index();

        return view($this->view, compact('typies','branchs'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->term->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $term = $this->term->show($id);

        if ($term instanceof JsonResponse && $term->getStatusCode() === 404) {
            return abort('404');
        }

        $typies = $this->type->index();

        $branchs = $this->branch->index();

        return view($this->view, compact('typies','branchs','term'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->term->renew($request, $id);
    }

    /**
     * 该学期报名用户
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function users(Request $request, $id)
    {
        $term = $this->term->show($id);

        if ($term instanceof JsonResponse && $term->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('term'));
    }


    /**
     * 培训计划详细内容
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function category(Request $request, $termId)
    {
        $term = $this->term->show($termId);

        if ($term instanceof JsonResponse && $term->getStatusCode() === 404) {
            return abort('404');
        }

        $categories = $term->categories;

        return view($this->view, compact('term','categories'));
    }

    /**
     * 新建课程
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function categoryCreate(Request $request, $termId)
    {
        $term = $this->term->show($termId);

        if ($term instanceof JsonResponse && $term->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('term'));
    }

    /**
     * 保存课程
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function categoryStore(Request $request, $termId)
    {
        return $this->category->store($request, $termId);
    }


    /**
     * 修改课程
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function categoryEdit(Request $request, $id)
    {
        $category = $this->category->show($id);

        return view($this->view, compact('category'));
    }

    /**
     * 更新课程
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function categoryUpdate(Request $request,$id)
    {
        return $this->category->renew($request, $id);
    }


    /**
     * 新建班级
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function claseCreate(Request $request, $termId)
    {
        $term = $this->term->show($termId);

        if ($term instanceof JsonResponse && $term->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('term'));
    }


    /**
     * 保存班级
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function claseStore(Request $request, $termId)
    {
        return $this->clase->store($request, $termId);
    }


    /**
     * 修改班级
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function claseEdit(Request $request, $id)
    {
        $clase = $this->clase->show($id);

        return view($this->view, compact('clase'));
    }

    /**
     * 更新班级
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function claseUpdate(Request $request, $id)
    {
        return $this->clase->renew($request, $id);
    }

    /**
     * 发布方案列表
     * @return [type] [description]
     */
    public function deploy()
    {
        $deploys = $this->deploy->index();

        return view($this->view, compact('deploys'));
    }


    /**
     * 新建发布方案
     * @return [type] [description]
     */
    public function deployCreate()
    {
        $terms = $this->term->deploy();

        return view($this->view, compact('terms'));
    }


    /**
     * 保存发布方案
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function deployStore(Request $request)
    {
        return $this->deploy->store($request);
    }


    /**
     * 修改发布方案
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function deployEdit(Request $request, $id)
    {
        $deploy = $this->deploy->show($id);

        if ($deploy instanceof JsonResponse && $deploy->getStatusCode() === 404) {
            return abort('404');
        }

        return view($this->view, compact('deploy'));
    }


    /**
     * 更新发布方案
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function deployUpdate(Request $request,$id)
    {
        return $this->deploy->renew($request, $id);
    }

    /**
     * 更改发布方案状态
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function deployChange(Request $request)
    {
        return $this->deploy->change($request);
    }


    /**
    *   查看用户上传的附件
    *
    */
    public function attachment($id)
    {
        $attachments = $this->tuser->att($id);

        return view($this->view, compact('attachments'));
    }



    public function add($tid)
    {
        $term = $this->term->show($tid);

        return view($this->view, compact('term'));
    }


    public function addUser(Request $request)
    {
        return $this->tuser->add($request);
    }


    public function export($tid)
    {
        return (new ClaseExport($tid))->download('报名'.date('Y-m-d',time()).'.xlsx');
    }


    /**
     * 批量设置用户报名状态状态
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function status(Request $request)
    {
        return $this->tuser->status($request);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
